import sys
from array import array
from socket import *
from struct import *

class tablet:
    tablet_id = None
    table_name = None
    start_row = None
    end_row = None
    status = None

def unpack_string(buf,offset):
    str_len = unpack_from('!I',buf,offset)[0]
    offset += calcsize("!I")

    #print 'str_len: %u' % str_len

    s = unpack_from(str(str_len)+'s', buf, offset)[0]

    return s,(calcsize("!I")+str_len)

def unpack_tablet(buf,offset):
    t = tablet()
    buf_offset = offset

    t.tablet_id = unpack_from('!Q', buf, buf_offset)[0]
    buf_offset += calcsize('!Q')

    #print 'tablet_id: %u' % t.tablet_id

    t.table_name,decode_len = unpack_string(buf, buf_offset)
    buf_offset += decode_len

    #print 'table_name: %s' % t.table_name

    t.start_row,decode_len = unpack_string(buf, buf_offset)
    buf_offset += decode_len

    #print 'start_row: %s' % t.start_row

    t.end_row,decode_len = unpack_string(buf, buf_offset)
    buf_offset += decode_len

    #print 'end_row: %s' % t.end_row

    t.status = unpack_from('!H', buf, buf_offset)[0]
    buf_offset += calcsize('!H')

    #print 'status: %u' % t.status

    return t,(buf_offset-offset)

def unpack_server_address(buf,offset):
    server_address_obj = server_address()

    server_address_obj.host_name,decode_len = unpack_string(buf,offset)
    offset += decode_len

    server_address_obj.port = unpack_from('!H',buf,offset)
    offset += calcsize('!H')

    server_address_obj.start_code = unpack_from('!L',buf,offset)
    offset += calcsize('!L')

    return server_address_obj

def send_packet(sock, pkt_buf, pkt_type, pkt_seq):
    buf = array('B', '\0'*4096)
    offset = 0

    # magic
    pack_into('!H', buf, offset, 0x11DE)
    offset += calcsize('!H')

    # version
    pack_into('!B', buf, offset, 1)
    offset += calcsize('!B')

    # type
    pack_into('!H', buf, offset, pkt_type)
    offset += calcsize('!H')

    # seq
    pack_into('!I', buf, offset, pkt_seq)
    offset += calcsize('!I')

    # length
    pack_into('!I', buf, offset, len(pkt_buf))
    offset += calcsize('!I')

    # packet
    pack_into(str(len(pkt_buf))+'s', buf, offset, pkt_buf)
    offset += len(pkt_buf)

    #print 'send packet: %s' % buf[:offset]
    sock.send(buf[:offset].tostring())

def recv_resp(sock):
    resp_buf = s.recv(4096)

    buf = array('B')
    buf.fromstring(resp_buf)
    #print 'response: %s' % buf

    offset = 0

    # magic
    magic = unpack_from('!H', resp_buf, offset)[0]
    offset +=calcsize('!H')

    #print 'magic: %u' % magic

    # version
    version = unpack_from('!B', resp_buf, offset)[0]
    offset += calcsize('!B')

    #print 'version: %u' % version

    # type
    t = unpack_from('!H', resp_buf, offset)[0]
    offset += calcsize('!H')

    #print 't: %u' % t

    # seq
    seq = unpack_from('!I', resp_buf, offset)[0]
    offset += calcsize('!I')

    #print 'seq: %u' % seq

    # length
    length = unpack_from('!I', resp_buf, offset)[0]
    offset += calcsize('!I')

    #print 'length: %u' % length

    # pkt
    pkt = unpack_from(str(length)+'s', resp_buf, offset)[0]
    offset += length

    return pkt

if len(sys.argv)!=3:
    print 'usage: %s hostname port'
    sys.exit(0)

hostname = sys.argv[1]
port = int(sys.argv[2])

# connect to tablet server
s = socket(AF_INET, SOCK_STREAM)
try:
    s.connect((hostname,port))
except:
    print 'cannot connet to master server'
    s.close()
    s = None
    sys.exit(-1)

#print 'connected to %s:%u' % (hostname,port)

# program version
pkt = pack('!I',1)

send_packet(s,pkt,0x28,0)
resp_pkt = recv_resp(s)

offset = 0

# error code
err_code = unpack_from('!I', resp_pkt, offset)[0]
offset += calcsize('!I')

if err_code != 0:
    print 'response error, err_code=%u' % err_code
    s.close()
    s = None
    sys.exit(-1)

# tablet number
tablet_num = unpack_from('!I', resp_pkt, offset)[0]
offset += calcsize('!I')

#print 'tablet_num: %u' % tablet_num

print 'table_name\ttablet_id\tstart_row\tend_row\tis_online\tis_split\tis_deleted'

# tablet infos
for i in range(tablet_num):
    t,unpack_len = unpack_tablet(resp_pkt, offset)
    offset += unpack_len

    is_online = 'yes'
    is_split = 'no'
    is_deleted = 'no'

    if len(t.start_row)==0:
        t.start_row = 'N/A'

    if len(t.end_row)==0:
        t.end_row = 'N/A'

    if t.status&1 == 0:
        is_online = 'no'

    if t.status&2 != 0:
        is_split = 'yes'

    if t.status&4 != 0:
        is_deleted = 'no'

    print '%s\t%u\t%s\t%s\t%s\t%s\t%s' \
        % (t.table_name, t.tablet_id, \
            t.start_row, t.end_row, \
            is_online, is_split, is_deleted)